<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Crypt;
use DB;
class UserController extends Controller
{

	public function index(Request $request)
	{
        
		$data=DB::table('users')
					->where("name","like","%".$request->input('search')."%" )
					->paginate($request->input('pages',10));
		// dd($request->all());
		return view('admin.user.index',['data'=>$data,'request'=>$request->all()]);
	}



    public function add()
    {
    	return view('admin.user.add');
    }
    



    public function insert(Request $request)
    {

    	$data=$request->except("_token","repassword");
    	// dd($data);
    	// dd($request->all());
    	$this->validate($request,[  
    		'name'=>'required|min:6|max:18',
    		'password'=>'required|',
    		'repassword'=>'same:password',
    		'email'=>'required|email'
    		],[
    		'name.required'=>'用户名不得为空',
    		'name.min'=>'用户名不得少于6位',
    		'name.max'=>'用户名不得大于18位',
    		'passwoed.required'=>'密码不得为空',
    		'repassword.same'=>'两次密码不一致',
    		'email.required'=>'邮箱不得为空',
    		'email.email'=>'邮箱格式不正确',
    		]);


	     
	     //token
	     $data['password']=Crypt::encrypt("{$data['password']}");
	     $data['token']=str_random(20);
	     $data['status']=1;
	     $res=DB::table('users')->insert($data);

	     if($res)
	     {
	     	return redirect('admin/user/index');
	     }else
	     {
	         // return redirect('admin/user/add');
	    	//  return back();
	     	return back()->with(['info'=>'添加失败']);
	     }
    }

    public function updatest(Request $request)
    {
    	$id=$request->id;
    	
    	$data=DB::table('users')->select('status')->where('id',$id)->first();

    	if($data->status==0)
    	{
    		// return 2222;
    		$res=DB::table('users')->where('id',$id)->update(['status'=>1]);
    		if($res)
    		{
    			return response()->json(1);
    		}else
    		{
    			return response()->json(5);
    		}
    	}elseif($data->status==1)
    	{
    		// return 1111;
    		$res=DB::table('users')->where('id',$id)->update(['status'=>0]);
    		if($res)
    		{

    			return response()->json(0);
    		}else
    		{
    			return response()->json(5);
    		}
    		
    	}




    }
//双击修改name
    public function updatena(Request $request)
    {
    	// return $request->all();
    	$id=$request->id;
    	$name=$request->name;
    	//检测名是否重复
    	$res=DB::table('users')->where('name',$name)->first();
    	if($res)
    	{
    		return response()->json(5);
    	}
    	//插入
    	$res=DB::table('users')->where('id',$id)->update(['name'=>$name]);
    	if($res)
    	{
    		return response()->json(1);
    	}else
    	{
    		return response()->json(0);
    	}



    }
//双击修改name
//编辑
	public function edit($id)
	{
		$data=DB::table('users')->where('id',$id)->first();
		
		if($data)
		{
			// echo 111;
			return view('admin.user.edit',['data'=>$data]);
		}else
		{

			return back()->with(['info'=>'编辑错误']);
		}

	}
//编辑
//编辑后更新
	public function updateall(Request $request)
	{
		$id=$request->id;
		$data=$request->except('_token','id');
		$res=DB::table('users')->where('id',$id)->update($data);
		if($res)
		{
			return redirect('admin/user/index')->with(['info'=>'修改成功']);
		}else
		{

		}


	}
//编辑后更新
//删除
   public function delete($id)
   {
   		
   		$res=DB::table('users')->delete($id);
   		if($res)
   		{
   			return back()->with(['info'=>'删除成功']);
   		}else
   		{
   			return back()->with(['info'=>'删除失败']);

   		}
   }



//删除


















}
